package com.example.ui_test.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View

/**
 *
 *
 * @Author: BigWhite
 * @CreateDate: 2021/4/27 11:23
 */
class BezierView(context: Context) : View(context) {
    private var paint: Paint
    private var path: Path

    private var path2 = Path()

    constructor(context: Context, attrs: AttributeSet) : this(context) {
    }

    init {
        paint = Paint()
        path = Path()
        paint.color = Color.BLUE
        paint.strokeWidth = 8f
        paint.style = Paint.Style.STROKE
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        path.moveTo(0f, 0f)
        path.quadTo(
            measuredWidth.toFloat() * 2 / 3,
            measuredHeight / 2f,
            0f,
            measuredHeight.toFloat()
        )
        var pathMeasure = PathMeasure(path, false)
        var pos = floatArrayOf(0f, 0f)
        pathMeasure.getPosTan(pathMeasure.length / 2, pos, null)
        path2.moveTo(0f, 0f)
        path2.lineTo(pos[0], pos[1])
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.drawPath(path, paint)
        canvas?.drawPath(path2, paint)
    }
}